package com.niit.ssmvc_chapter04.dao.impl;

import com.niit.ssmvc_chapter04.dao.StudentDao;
import com.niit.ssmvc_chapter04.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author yaojianfeng
 */
@Repository("studentDaoUseTemplate")
public class StudentDaoUseTemplateImpl implements StudentDao {

    @Autowired
    private JdbcTemplate template;

    private final String tableName = "student";
    private final String baseField = "(sid,name,english_name,address,phone_number,birthday)";

    @Override
    public int insert(Student student) {
        String sql= "insert into " + tableName + baseField +
                " values (" + "'" + student.getSid()
                + "','" + student.getName()
                + "','" + student.getEnglishName()
                + "','" + student.getAddress()
                + "'," + student.getPhoneNumber()
                + ",'" + student.getBirthday()
                + "');";
        return template.update(sql);
    }

    @Override
    public int queryTotalCount() {
        String sql = "select count(*) from student";
        return template.queryForObject(sql,int.class);
    }
}
